<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="类别代号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入类别代号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="索引ID" prop="indexId">
        <el-input
          v-model="queryParams.indexId"
          placeholder="请输入索引ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父ID" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="索引全名称" prop="indexName">
        <el-input
          v-model="queryParams.indexName"
          placeholder="请输入索引全名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="等级" prop="nlevel">
        <el-input
          v-model="queryParams.nlevel"
          placeholder="请输入等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件编号" prop="filenum">
        <el-input
          v-model="queryParams.filenum"
          placeholder="请输入文件编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
<!--        v-hasPermi="['system:treemain:add']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="treemainList"
      row-key="indexId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="索引ID" prop="indexId" />
      <el-table-column label="父ID" align="center" prop="parentId" />
      <el-table-column label="类别代号" align="center" prop="id" />
      <el-table-column label="索引全名称" align="center" prop="indexName" />
      <el-table-column label="等级" align="center" prop="nlevel" />
      <el-table-column label="编号" align="center" prop="num" />
      <el-table-column label="文件编号" align="center" prop="filenum" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
<!--          v-hasPermi="['system:treemain:edit']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"

          >新增</el-button>
<!--          v-hasPermi="['system:treemain:add']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
<!--          v-hasPermi="['system:treemain:remove']"-->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改档案类别对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body :close-on-click-modal='false'>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px" >
        <el-form-item label="索引ID" prop="indexId">
          <el-input v-model="form.indexId" placeholder="请输入索引ID" disabled />
        </el-form-item>
        <el-form-item label="父ID" prop="parentId">
          <treeselect v-model="form.parentId" :options="treemainOptions" :normalizer="normalizer" placeholder="请选择父ID" />
        </el-form-item>
        <el-form-item label="索引全名称" prop="indexName">
          <el-input v-model="form.indexName" placeholder="请输入索引全名称" />
        </el-form-item>
        <el-form-item label="等级" prop="nlevel">
          <el-input v-model="form.nlevel" placeholder="请输入等级" />
        </el-form-item>
        <el-form-item label="编号" prop="num">
          <el-input v-model="form.num" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="文件编号" prop="filenum">
          <el-input v-model="form.filenum" placeholder="请输入文件编号" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="form.content" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTreemain, getTreemain, delTreemain, addTreemain, updateTreemain } from "@/api/system/treemain";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Treemain",
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 档案类别表格数据
      treemainList: [],
      // 档案类别树选项
      treemainOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        id: null,
        indexId: null,
        parentId: null,
        indexName: null,
        nlevel: null,
        num: null,
        filenum: null,
        content: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "类别代号不能为空", trigger: "blur" }
        ],
        // indexId: [
        //   { required: true, message: "索引ID不能为空", trigger: "blur" }
        // ],
        parentId: [
          { required: true, message: "父ID不能为空", trigger: "blur" }
        ],
        indexName: [
          { required: true, message: "索引全名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询档案类别列表 */
    getList() {
      this.loading = true;
      listTreemain(this.queryParams).then(response => {
        this.treemainList = this.handleTree(response.data, "indexId", "parentId");
        this.loading = false;
      });
    },
    /** 转换档案类别数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.indexId,
        label: node.indexName,
        children: node.children
      };
    },
	/** 查询档案类别下拉树结构 */
    getTreeselect() {
      listTreemain().then(response => {
        this.treemainOptions = [];
        const data = { indexId: -1, indexName: '咸阳市档案分类', children: [] };
        data.children = this.handleTree(response.data, "indexId", "parentId");
        this.treemainOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        indexId: null,
        parentId: null,
        indexName: null,
        nlevel: null,
        num: null,
        filenum: null,
        content: null,
        vflag: null,
        sindexName: null,
        ztype: null,
        content2: null,
        topnode: null,
        nodeico: null,
        nodetype: null,
        fromid: null,
        listno: null,
        frmtypeindex: null,
        showid: null,
        showfrmid: null,
        showfrmindexId: null,
        commIndex: null,
        baseIndex: null,
        commListno: null,
        projListno: null,
        intnoshow: null,
        projParent: null,
        treeDliststr: null,
        jid: null,
        flid: null,
        ctypeId: null,
        treemainReserved1: null,
        treemainReserved2: null,
        treemainReserved3: null,
        treemainReserved4: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.indexId) {
        this.form.parentId = row.indexId;
      } else {
        this.form.parentId = -1;
      }
      this.open = true;
      this.title = "添加档案类别";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.indexId;
      }
      getTreemain(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改档案类别";
      });
    },
    /** 提交按钮 */
    async submitForm() {
      console.log(this.form)
      this.$refs["form"].validate(async valid => {
        if (valid) {
          if (this.form.id != null) {
            await updateTreemain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            await addTreemain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除档案类别编号为"' + row.id + '"的数据项？').then(function() {
        return delTreemain(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
